<template>
  <div class="signal-card" :class="{ 'is-all': !isAll }">
    <div class="line title" @click="gotoKline">
      <div class="stock">
        <span>{{ stockInfo['name'] }}</span>
        <span class="code">{{ stockInfo['stock_code'] }}</span>
      </div>
      <div class="stock">
        <span>{{ stockInfo['nowPrice'] }}</span>
        <span :class="getColorClass(stockInfo.upDownPercent)">{{
          formatPercent(stockInfo.upDownPercent, 2, false)
        }}</span>
        <svg-icon icon="arrow-right" class="icon-arrow"></svg-icon>
      </div>
    </div>
    <div class="line">
      <div class="tags">
        <div class="tag" :class="{ up: stockInfo.range_rise > 0, down: stockInfo.range_rise < 0 }">
          区间涨幅 {{ formatPercent(stockInfo.range_rise, 2, false) }}
        </div>
        <div
          class="tag"
          :class="{ up: stockInfo.maximum_rise > 0, down: stockInfo.maximum_rise < 0 }"
        >
          最高涨幅 {{ formatPercent(stockInfo.maximum_rise, 2, false) }}
        </div>
      </div>
      <div class="btn" v-if="!isAll" @click="onShowAll">{{ buttonText }}</div>
    </div>
    <!-- 信号追踪 -->
    <div class="track" :class="{ border: isAll }">
      <div class="sub-title" v-if="hasSubTitle">信号追踪：</div>
      <div v-for="item in signals" :key="item.id" class="item">
        <div class="item-point" :style="trackPointStyle(item)">
          {{ trackPointText(item) }}
        </div>
        <div class="item-line"></div>
        <div class="item-content">
          <div class="stock-name">
            {{ item.push_content }}
          </div>
          <div v-if="item[sortTimeType]" class="date">
            {{ timestampToTime(item[sortTimeType], 3) }}
          </div>
          <div v-if="item.reason" class="long-msg" v-html="item.reason"></div>
        </div>
      </div>
      <div class="musk" v-if="!isAll"></div>
    </div>
    <!-- 全部信号 -->
    <atom-popup v-model="showAll" round closeable position="bottom" height-level="high">
      <div class="all-signal">
        <p>信号追踪</p>
        <tracking-card
          :signals="signals"
          :isAll="true"
          :stockInfo="stockInfo"
          :sortTimeType="sortTimeType"
        ></tracking-card>
      </div>
    </atom-popup>
  </div>
</template>

<script>
import { getColorClass, formatPercent, jumpTokline, timestampToTime } from '@/utils';
import { TRADE_STATUS } from '@/config/portfolio';
import TrackingCard from '@/components/portfolio/TrackingCard.vue';

export default {
  name: 'tracking-card',
  components: {
    TrackingCard,
  },
  props: {
    signals: {
      type: Array,
      default: () => [],
    },
    isAll: {
      type: Boolean,
      default: false,
    },
    stockInfo: {
      type: Object,
      default: () => ({}),
    },
    // 因历史原因存在多条信号线的情况，需要区分使用哪个字段排序。后续多按照push_time排序
    sortTimeType: {
      type: 'trade_time' | 'push_time',
      default: 'trade_time',
    },
    // 展开按钮文案
    buttonText:{
      type:String,
      default:'详情'
    },
    // 是否有信号追踪的副标题
    hasSubTitle:{
      type:Boolean,
      default:false
    }
  },
  data() {
    return {
      showAll: false,
      stockStatusColor: TRADE_STATUS,
    };
  },
  methods: {
    getColorClass,
    formatPercent,
    timestampToTime,
    onShowAll() {
      this.showAll = true;
    },
    gotoKline() {
      const { stock_code, market } = this.stockInfo;
      if (stock_code && market) {
        jumpTokline(stock_code, market);
      }
    },
    trackPointStyle(item) {
      const pointStyleOptions = this.stockStatusColor?.[item.type]?.[item.status || 'default'] || {
        color: '#FFB30',
        borderColor: '',
        bgColor: 'rgba(255,179,0,0.20)',
      };
      return {
        color: pointStyleOptions.color,
        border: pointStyleOptions.borderColor
          ? `1px solid ${pointStyleOptions.borderColor}`
          : 'none',
        background: pointStyleOptions.bgColor,
      };
    },
    trackPointText(item) {
      return this.stockStatusColor?.[item.type]?.[item.status || 'default']?.name || '';
    },
  },
};
</script>

<style lang="less" scoped>
.signal-card {
  box-sizing: border-box;
  height: 246px;
  border-radius: 6px;
  &.is-all {
    background: var(--bg-color-4);
    margin-bottom: 8px;
  }
  font-size: 12px;
  padding: 16px 6px 12px;
}

.line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 4px 12px;
}

.title {
  font-size: 16px;
  line-height: 20px;
  font-weight: 600;
  color: var(--text-1);
  .stock {
    display: flex;
    align-items: center;
    .code {
      font-size: 14px;
      font-weight: 400;
      color: var(--text-3);
    }
    :first-child {
      margin-right: 12px;
    }
  }
}

.icon-arrow {
  margin-left: 8px;
}

.tags {
  display: flex;
  align-items: center;
  :first-child {
    margin-right: 8px;
  }
  .tag {
    font-size: 11px;
    line-height: 14px;
    height: 14px;
    font-weight: 400;
    padding: 0 4px;
    border: 0.33px solid #000;
    border-radius: 2px;
    &.up {
      color: #ff2436;
      border-color: #ff2436;
    }
    &.down {
      color: #07ab4b;
      border-color: #07ab4b;
    }
  }
}

.btn {
  font-size: 14px;
  color: #4167d9;
  letter-spacing: 0;
  line-height: 18px;
  font-weight: 400;
}

.track {
  background-image: linear-gradient(
    180deg,
    var(--bg-gradient-1) 0%,
    var(--bg-gradient-2) 55%,
    var(--bg-gradient-3) 100%
  );
  border-radius: 4px;
  padding: 12px 8px;
  padding-right: 0;
  height: 158px;
  overflow-y: hidden;
  box-sizing: border-box;
  position: relative;
  &.border {
    border-top: 1px solid var(--text-4);
    margin-top: 16px;
    padding-top: 16px;
    height: auto;
  }
  .title {
    font-size: 14px;
    color: var(--text-1);
    line-height: 18px;
    font-weight: 600;
    margin-bottom: 12px;
  }
  .sub-title{
    font-size: 12px;
    color: var(--text-1);
    line-height: 18px;
    font-weight: 600;
    margin-bottom: 12px;
  }
  .musk {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 36px;
    background-image: linear-gradient(
      180deg,
      var(--bg-gradient-4) 18%,
      var(--bg-gradient-5) 74%,
      var(--bg-gradient-5) 74%
    );
    border-radius: 0 4px 4px;
  }
}

.item {
  position: relative;
  margin-bottom: 4px;
  .item-point {
    position: absolute;
    width: 18px;
    height: 18px;
    font-size: 12px;
    text-align: center;
    line-height: 16px;
    box-sizing: border-box;
    border-radius: 2px;
  }
  .item-line {
    position: absolute;
    width: 0;
    border: 0.5px dashed #b2b2b2;
    height: calc(100% - 28px);
    top: 24px;
    left: 8.5px;
  }
  .item-content {
    display: flex;
    flex-direction: column;
    padding-left: 24px;
    box-sizing: border-box;
    padding-bottom: 8px;
    .stock-name {
      font-size: 14px;
      line-height: 18px;
      font-weight: 500;
      color: var(--text-1);
      margin-bottom: 4px;
    }
    .date {
      font-size: 12px;
      color: var(--text-2);
      line-height: 16px;
      font-weight: 400;
      margin-bottom: 6px;
    }
    .long-msg {
      font-size: 12px;
      line-height: 20px;
      font-weight: 400;
      color: var(--text-2);
      width: 100%;
      overflow: hidden;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
    }
  }
}

.all-signal {
  display: flex;
  flex-direction: column;
  height: 100%;
}

p {
  font-size: 18px;
  color: var(--text-1);
  text-align: center;
  line-height: 22px;
  font-weight: 500;
  margin: 20px 0;
}
</style>
